package com.linln.wtp.controller.integralMall;

import com.linln.common.utils.SessionUtil;
import com.linln.common.vo.JsonResult;
import com.linln.component.wtpshiro.annotation.PassToken;
import com.linln.component.wtpshiro.annotation.UserLoginToken;
import com.linln.modules.integralmall.service.IntegralMallServiceService;
import com.linln.modules.integralmall.service.ServiceTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.wtp.controller.integralMall
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.wtp.controller.integralMall
 * @Description: TODO
 * @date Date : 2020年12月17日 13:39
 */
@RestController
@RequestMapping("integralMall")
@Slf4j
public class IntegralMallController {
    @Autowired
    private IntegralMallServiceService integralMallServiceService;
    @Autowired
    private ServiceTypeService integralMallServiceTypeService;

    /**
     * 获取积分商城列表
     * @param pageNum 页码
     * @param limit 条数
     * @param serviceTypeId 类型id
     * @param integral 积分排序
     * @param integralMallServiceName 服务名称搜索
     * @return
     */
    @GetMapping
    @PassToken
    public Map<String, Object> getIntegralMall(@RequestParam(required = false, defaultValue = "0", value = "pageNum") Integer pageNum, @RequestParam(required = false, defaultValue = "10") Integer limit, String serviceTypeId, Integer integral
            , @RequestParam(value = "search", required = false) String integralMallServiceName) {
        Map<String, Object> map;
        try {
            map = integralMallServiceService.getIntegralMallList(pageNum, limit, serviceTypeId, integral, integralMallServiceName);
            map.put("code", 200);
            map.put("msg", "获取积分商城列表成功");
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            map = new HashMap<>();
            map.put("code", 500);
            map.put("msg", "获取积分商城列表失败");
            log.error(e.getMessage());
            return map;
        }
    }

    /**
     * 获取积分商城类型列表
     * @return
     */
    @GetMapping("/serviceType")
    @PassToken
    public Map<String, Object> getIntegralMallType() {
        Map<String, Object> map = new HashMap<>();
        try {
            map.put("data", integralMallServiceTypeService.getIntegralMallServiceTypeList());
            map.put("code", 200);
            map.put("msg", "获取积分服务类型成功");
        } catch (Exception e) {
            map = new HashMap<>();
            map.put("code", 500);
            map.put("msg", "获取积分服务类型失败");
            log.error(e.getMessage());
        }
        return map;
    }


    /**
     * 获取商品详情
     * @param serviceId 商品id
     * @return
     */
    @GetMapping("/serviceInfo")
    @PassToken
    public Map<String, Object> getIntegralMallServiceInfo(@RequestParam("serviceId") String serviceId) {
        Map<String, Object> map;
        try {
            map = integralMallServiceService.getIntegralMallServiceInfo(serviceId);
            map.put("code", 200);
            map.put("msg", "获取服务详情成功");
        } catch (Exception e) {
            map = new HashMap<>();
            map.put("code", 500);
            map.put("msg", "获取服务详情失败");
            log.error(e.getMessage());
        }
        return map;
    }

    /**
     * 购买商品
     * @param serviceId
     * @return
     */
    @UserLoginToken
    @PostMapping("/buyIntegralService")
    public JsonResult buyIntegralService(String serviceId) {
        try {
            return integralMallServiceService.buyIntegralService(serviceId, SessionUtil.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 关闭支付
     * @param serviceId
     * @return
     */
    @UserLoginToken
    @PostMapping("/closeIntegralService")
    public JsonResult closeIntegralService(String serviceId) {
        try {
            return integralMallServiceService.closeIntegralService(serviceId, SessionUtil.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }

    /**
     * 待支付支付
     * @param orderId
     * @return
     */
    @UserLoginToken
    @PostMapping("/payIntegralService")
    public JsonResult payIntegralService(String orderId) {
        try {
            return integralMallServiceService.payIntegralService(orderId, SessionUtil.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error(e.getMessage());
        }
    }
}
